
 /*********************************************************
 *
 *  Project:      HZFlashCards
 *
 *  Class:        HTMLListPane
 *
 *  File name:    HTMLListPane.java
 *
 *
 *    SVN:
 *
 *    $Rev$:
 *    $Date$:
 *    $LastChangedBy$:
 *
 *    $HeadURL$:
 *
 **********************************************************/

package com.eastmountain.chinese.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;

import com.eastmountain.chinese.common.HTMLStrings;
import com.eastmountain.chinese.gui.controllers.HTMLMenuBar;

/*********************************************************
 *
 *  Class:        HTMLListPane
 *
 **********************************************************/

@SuppressWarnings("serial")
public abstract class HTMLListPane extends JEditorPane {

    private HTMLMenuBar htmlMenuBar;

    private JScrollPane listScrollPane;
    private JScrollBar listScrollBar;

    StringBuilder htmlBody = new StringBuilder( 500 );

    public HTMLListPane() {

        System.out.println( "\nHTMLListPane() invoked\n" );

    }

    protected void createHTMLListPanel( String title ) {

        // TODO put this in a scroll pane

        System.out.println( "\nHTMLListPane.createHTMLListPanel() invoked\n" );

        JFrame frame = new JFrame( title );

        frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );

        listScrollPane = new JScrollPane();
        listScrollPane.setPreferredSize( new Dimension( 500, 200 ) );
        //listScrollBar = listScrollPane.getVerticalScrollBar();
        //JViewport viewport = listScrollPane.getViewport();
        //viewport.setViewPosition( new Point( 0, 0 ) );

        JPanel holderPanel = new JPanel();
        holderPanel.add( listScrollPane );
        //holderPanel.setPreferredSize( new Dimension( 500, 200 ) );

        htmlMenuBar = new HTMLMenuBar( this );

        frame.setJMenuBar( htmlMenuBar );

        frame.getContentPane().add( listScrollPane, BorderLayout.CENTER );

        frame.pack();
        frame.setVisible( true );

    }

    protected void startHTML() {

        setContentType( "text/html" );

        htmlBody.append( HTMLStrings.css + HTMLStrings.bodyStart + HTMLStrings.nl );

    }

    protected void finishHTML() {

        htmlBody.append( HTMLStrings.bodyEnd + HTMLStrings.htmlEnd );

        setText( HTMLStrings.htmlTextComponentBegin + htmlBody.toString() );

        listScrollPane.getViewport().add( this );
        
        //  EXP table print experiment
        
        //listScrollPane.getViewport().add( new SimpleTableSelectionDemo() );
        
        // TODO how to get the scrollpane to go to the top

        //listScrollBar.setValue( 0 );
        //listScrollPane.repaint();

    }

    public void printHTML() {

        // TODO example of use of MessageFormat

        //MessageFormat headerFormat
        //    = new MessageFormat( "{integer}" );

        print( this.getGraphics() );

    }

    public void writeHTML() {

        String htmlOut = HTMLStrings.htmlFileBegin + htmlBody.toString();

        File outputFile =  getFileName();

        try {

            FileOutputStream fileOutputStream = new FileOutputStream( outputFile.getPath() );
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter( fileOutputStream, "UTF-8" );

            outputStreamWriter.write( htmlOut );

            outputStreamWriter.close();

        } catch ( IOException e ) {

            e.printStackTrace();


        } catch ( Exception e ) {

            System.out.println( "\nMysterious exception: " + e.getMessage() );

            e.printStackTrace();

        }

    }

    private File getFileName() {

        int returnVal;
        File outputFile = null;

            FileChooser fileChooser = new FileChooser( JFileChooser.SAVE_DIALOG, "Save HTML" );

            returnVal = fileChooser.showSaveDialog( null );

            if ( returnVal == JFileChooser.APPROVE_OPTION) {

                outputFile = fileChooser.getSelectedFile();

                System.out.println( "\nOpening: " + outputFile + "\n" );

            } else {

                System.out.println( "\nOpen command cancelled by user.\n" );

            }

            return outputFile;

    }

}
